//
//  LCCardCameraView.swift
//  dj-learn-Swift
//
//  Created by jie sun on 2022/5/19.
//  Copyright © 2022 S.J. All rights reserved.
//

import UIKit

let KScreenWidth = UIScreen.main.bounds.width
let KScreenHeight = UIScreen.main.bounds.height

class LCCardCameraView: UIView {
    // 方框图片
    var boxImgView = UIImageView()
    // 拍照按钮
    var takePhotoButton = UIButton(type: .custom)
    // 手电筒
    var torchButton = UIButton(type: .custom)
    // 关闭按钮
    var closeButton = UIButton(type: .custom)
    
    convenience init() {
        self.init(frame: .zero)
        self.configSubviews()
        self.makeSubviewsConstraints()
    }
    
    private func configSubviews() {
        self.boxImgView.image = UIImage(named: "card_carema_box")
        
        self.closeButton.setImage(UIImage(named: "card_carema_close"), for: .normal)
        
        self.takePhotoButton.setImage(UIImage(named: "card_carema_take_photo"), for: .normal)
        
        self.torchButton.setImage(UIImage(named: "card_carema_torch"), for: .normal)
    }
    
    private func makeSubviewsConstraints() {
        self.addSubview(self.takePhotoButton)
        self.takePhotoButton.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-20.0)
            make.size.equalTo(CGSize(width: 68.0, height: 68.0))
        }
        
        self.addSubview(self.torchButton)
        self.torchButton.snp.makeConstraints { make in
            make.centerY.equalTo(self.takePhotoButton)
            make.left.equalTo(self)
            make.size.equalTo(CGSize(width: 60.0, height: 60.0))
        }
        
        self.addSubview(self.closeButton)
        self.closeButton.snp.makeConstraints { make in
            make.right.equalTo(self).offset(-10.0)
            make.top.equalTo(self).offset(10.0)
            make.size.equalTo(CGSize(width: 60.0, height: 60.0))
        }
        
        var multiplied = 1.58
        if let image = UIImage(named: "card_carema_box") {
            multiplied = image.size.height / image.size.width
        }
        self.addSubview(self.boxImgView)
        self.boxImgView.snp.makeConstraints { make in
            make.bottom.equalTo(self.takePhotoButton.snp.top).offset(-30.0)
            make.left.equalTo(self).offset(60.0)
            make.right.equalTo(self).offset(-60.0)
            make.height.equalTo(self.boxImgView.snp.width).multipliedBy(multiplied)
        }
    }
}
